package com.huiyeji.sort.utils;

import com.huiyeji.sort.Order;
import com.huiyeji.sort.Sortable;

public class OrderUtil {

    public static int getOrder(Object object){
        if (object instanceof Sortable){
            Sortable sortable = (Sortable) object;
            int order = sortable.getOrder();
            if (order < 0){
                return getOrderByAnnotation(object);
            }
            return order;
        } else {
            return getOrderByAnnotation(object);
        }
    }

    private static int getOrderByAnnotation(Object object){
        Order anno = object.getClass().getAnnotation(Order.class);
        return anno == null ? -1 : anno.value();
    }
}
